import { useStore } from 'vuex';
import store from '../store';
import router from './index';
import storage from '../utils/storage';

// const store = useStore();
let registerRouteFresh = true;
const whiteList = ['/login', '/404']; // no redirect whitelist
router.beforeEach(async (to, from, next) => {
  if (storage.getItem('token')) {
    if (registerRouteFresh) {
      console.log(store);
      await store.dispatch('user/getMenuList');
      next({ ...to, replace: true });
      registerRouteFresh = false;
    } else {
      next();
    }
  } else if (whiteList.indexOf(to.path) !== -1) {
    next(); 
  } else {
    next({
      path: '/login',
      query: {
        redirect: to.fullPath,
      },
    }); 
  }
});